import {
  activityList
} from '../../apis/activity';
import {
  splicing,
  handleDate
} from '../../utils/util';
const app = getApp();

Page({
  data: {
    height: wx.getMenuButtonBoundingClientRect().height + wx.getMenuButtonBoundingClientRect().top + 6,
    activityList: [],
    pageNum: 1,
    isRequest: false,
    isHaveMore: true,
    searchKeyword: '',
    searchList: [],
    searchPageNum: 1,
    searchHaveMore: true,
    isSearch: false,
    isRefresh: false
  },

  onLoad() {
    this.getActivityList(1);
  },

  onShareAppMessage() {
    return {
      title: '北京市移民及出入境服务行业协会',
      path: '/pages/start/index',
      imageUrl: '/images/logo.png'
    }
  },

  // scroll-view 触底
  bindscrolltolower() {
    if (this.data.isSearch) {
      if (this.data.searchHaveMore && !this.data.isRequest) {
        this.searchActivity(this.data.searchPageNum + 1, this.data.searchKeyword, 'load');
      };
    } else {
      if (this.data.isHaveMore && !this.data.isRequest) {
        this.getActivityList(this.data.pageNum + 1, 'load');
      };
    };
  },
  // scroll-view 下拉
  bindrefresherrefresh(e) {
    this.setData({
      isRefresh: true,
      searchPageNum: 1,
      searchHaveMore: true,
      pageNum: 1,
      isHaveMore: true
    });
    if (this.data.isSearch) {
      this.searchActivity(1, this.data.searchKeyword);
    } else {
      this.getActivityList(1);
    };
  },

  getActivityList(pageNum, type) {
    if (this.data.isRequest) return;
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    this.setData({
      isRequest: true
    });
    activityList(pageNum, 10, '').then(res => {
      wx.hideLoading();
      this.setData({
        isRequest: false,
        isRefresh: false
      });
      if (res.data.code === 200) {
        this.setData({
          pageNum: pageNum
        });
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          v.imageList = v.imageList.map(val => val = splicing(val));
          v.enrollStartTime = handleDate(v.enrollStartTime);
          v.enrollCloseTime = handleDate(v.enrollCloseTime);
          v.startTime = handleDate(v.startTime);
          v.closeTime = handleDate(v.closeTime);
        });
        if (type === 'load') {
          rows.forEach(v => {
            this.data.activityList.push(v);
          });
          this.setData({
            activityList: this.data.activityList
          });
        } else {
          this.setData({
            activityList: rows
          });
        };

        if (this.data.activityList.length === total) {
          this.setData({
            isHaveMore: false
          });
        };
      };
    });
  },

  searchActivity(pageNum, keyWord, type) {
    if (this.data.isRequest) return;
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    this.setData({
      isRequest: true
    });
    activityList(pageNum, 10, keyWord).then(res => {
      wx.hideLoading();
      this.setData({
        isRequest: false,
        isRefresh: false
      });
      if (res.data.code === 200) {
        this.setData({
          searchPageNum: pageNum
        });
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          v.imageList = v.imageList.map(val => val = splicing(val));
          v.enrollStartTime = handleDate(v.enrollStartTime);
          v.enrollCloseTime = handleDate(v.enrollCloseTime);
          v.startTime = handleDate(v.startTime);
          v.closeTime = handleDate(v.closeTime);
        });
        if (type === 'load') {
          rows.forEach(v => {
            this.data.searchList.push(v);
          });
          this.setData({
            searchList: this.data.searchList
          });
        } else {
          this.setData({
            searchList: rows
          });
        };

        if (this.data.searchList.length === total) {
          this.setData({
            searchHaveMore: false
          });
        };
      };
    });
  },

  // 活动详情
  activityDetails(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/activityDetails/index?activityId=${id}`
    });
  },

  // 搜索
  handleSearch(e) {
    let {
      value
    } = e.detail;
    this.setData({
      searchPageNum: 1,
      searchHaveMore: true,
      searchKeyword: value,
      isSearch: true
    });
    this.searchActivity(1, value);
  },

  // 取消搜索
  cancelSearch() {
    this.setData({
      searchKeyword: '',
      searchList: [],
      searchPageNum: 1,
      searchHaveMore: true,
      isSearch: false
    });
  }
})